using QFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;

public class UnityPoolController : MonoBehaviour, IController
{
    [SerializeField]
    PoolInGame cargoContainers_Pool;
    [SerializeField]
    PoolInGame cargo_1200_1200_1050s_Pool;
    // Start is called before the first frame update
    void Awake()
    {
        this.RegisterEvent<InitGamePoolsEvent>(OnInitGamePools).UnRegisterWhenGameObjectDestroyed(this);
        //poolA.SetPrefab(this.GetModel<IGameModel>().CargoPrefab.transform);
        //poolB.SetPrefab(this.GetModel<IGameModel>().Cargo_1200_1200_1050_Prefab.transform);
    }

    private void OnInitGamePools(InitGamePoolsEvent e)
    {
        if (!cargoContainers_Pool)
        {
            GameObject poolContainer = new GameObject();
            poolContainer.transform.parent = transform;
            cargoContainers_Pool = poolContainer.AddComponent<PoolInGame>();
        }
        if (!cargo_1200_1200_1050s_Pool)
        {
            GameObject poolContainer = new GameObject();
            poolContainer.transform.parent = transform;
            cargo_1200_1200_1050s_Pool = poolContainer.AddComponent<PoolInGame>();
        }
        PoolsInGame.CargoContainers = cargoContainers_Pool;
        PoolsInGame.Cargo_1100_1100_1400s = cargo_1200_1200_1050s_Pool;
    }

    public IArchitecture GetArchitecture()
    {
        return DigitalTwin_GeLinMei_2.Interface;
    }
}
public interface IBasicPool
{
    public void Initialize(bool collectionCheck = true);
}
public abstract class BasicPool<T> : MonoBehaviour, IBasicPool where T : Component
{
    [SerializeField]
    protected T prefab;
    [SerializeField]
    int defaultSize = 100;
    [SerializeField]
    int maxSize = 500;

    ObjectPool<T> pool;
    public void Initialize(bool collectionCheck = true) =>
        pool = new ObjectPool<T>(createFunc: OnCreatePoolItem, actionOnGet: OnGetPoolItem, actionOnRelease: OnReleasePoolItem,
            actionOnDestroy: OnDestroyPoolItem, collectionCheck: collectionCheck, defaultSize, maxSize);

    protected abstract T OnCreatePoolItem();
    protected abstract void OnGetPoolItem(T obj);
    protected abstract void OnReleasePoolItem(T obj);
    protected abstract void OnDestroyPoolItem(T obj);

    public void SetPrefab(T prefab)
    {
        this.prefab = prefab;
        this.name = $"Pool : {this.prefab.name}";
    }

    public T Get() => pool.Get();
    public void Release(T obj) => pool.Release(obj);
    public void Clear() => pool.Clear();
}
